@extends('admin::layouts.master')
@section('title')
    礼包设置
@endsection
@section('tab')

    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form ">
                {{csrf_field()}}
                {{hi_form_hidden('type',$type)}}
                @if( isset( $info['id'] ))
                {{hi_form_hidden('id',$info['id'])}}
                @endif
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>礼包名称
                    </label>
                    <div class="layui-input-inline" style="width: 300px;">
                        <input type="text" id="name" name="title" placeholder="请输入标题" @if( isset( $info['title'] )) value="{{$info['title']}}" @endif autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>礼包类型
                    </label>
                    <div class="layui-input-inline" style="width: 300px;">
                        @if( $type == 1 )
                            <input type="text"  placeholder="新用户注册礼包" value="新用户注册礼包" disabled="disabled" autocomplete="off" class="layui-input">
                        @elseif( $type == 2 )
                            <input type="text"  placeholder="老用户回馈礼包" value="老用户回馈礼包" disabled="disabled" autocomplete="off" class="layui-input">
                        @else
                            <input type="text"  placeholder="邀请回馈礼包" value="邀请回馈礼包" disabled="disabled" autocomplete="off" class="layui-input">
                        @endif
                    </div>
                </div>

                @if( $type == 2 || $type == 4 || $type == 5)
                    <div class="layui-form-item">
                        <label for="name" class="layui-form-label">
                            <span class="x-red">*</span>用户年限
                        </label>
                        <div class="layui-input-inline" style="width: 300px;">
                            <select name="age" lay-filter="age">
                                <option value="">请选择用户年限</option>
                                <option value="2" @if( isset( $info['age'] ) && $info['age'] == 2 ) selected="selected" @endif>二年老用户</option>
                                <option value="3" @if( isset( $info['age'] ) && $info['age'] == 3 ) selected="selected" @endif>三年老用户</option>
                                <option value="5" @if( isset( $info['age'] ) && $info['age'] == 5 ) selected="selected" @endif>五年老用户</option>
                            </select>
                        </div>
                    </div>
                @elseif( $type == 3 )
                    <div class="layui-form-item">
                        <label for="name" class="layui-form-label">
                            <span class="x-red">*</span>邀请人数
                        </label>
                        <div class="layui-input-inline" style="width: 300px;">
                            <input type="number" min="1" name="number" @if( isset( $info['number'] )) value="{{$info['number']}}" @endif  placeholder="请输入邀请人数" value="" class="layui-input">
                        </div>
                    </div>
                @endif

                <hr/>
                <div class="layui-form-item">
                    <label class="layui-form-label">满减券</label>
                    <div class="layui-input-inline">
                        <select name="type_coupon_manjian" lay-filter="manjian_type_coupon">
                            <option value="">请选择使用类型</option>
                            <option value="1" @if( isset( $info['manjian']['type_shop'] ) && $info['manjian']['type_shop'] == 1 ) selected="selected" @endif >普通商品</option>
                            <option value="2" @if( isset( $info['manjian']['type_shop'] ) && $info['manjian']['type_shop'] == 2 ) selected="selected" @endif >服务商品</option>
                            <option value="3" @if( isset( $info['manjian']['type_shop'] ) && $info['manjian']['type_shop'] == 3 ) selected="selected" @endif >保养套餐</option>
                        </select>
                    </div>
                    <div>
                        <table class="layui-table layui-form">
                            <thead>
                            <tr>
                                <th>选择</th>
                                <th>优惠券名称</th>
                                <th>赠送数量（张）</th>
                                <th>有效期限（天）</th></tr>
                            </thead>
                            <tbody id="manjian_tr">
                                @if( isset( $info['manjian'] ) )
                                    @foreach( $info['manjian']['coupons'] as $key=>$vo)
                                        <tr class="phono_ine">
                                            <td><input type="checkbox" name="manjian[]" value="{{$vo['id']}}" @if( !empty( $vo['number']) ) checked="checked" @endif lay-skin="primary" placeholder="赠送张数" autocomplete="off"></td>
                                            <td>{{$vo['name']}}</td>
                                            <td><input type="number" min="1" name="manjian_number[]" placeholder="赠送张数" value="{{$vo['number']}}" autocomplete="off" class="layui-input"></td>
                                            <td><input type="number" min="1"  name="manjian_validity_at[]" placeholder="有效期限" value="{{$vo['validity_at']}}" autocomplete="off" class="layui-input"></td>
                                        </tr>
                                    @endforeach
                                @endif
                            </tbody>
                        </table>
                    </div>
                </div>
                <hr/>
                <div class="layui-form-item">
                    <label class="layui-form-label">折扣券</label>
                    <div class="layui-input-inline">
                        <select name="type_coupon_zhekou" lay-filter="zhekou_type_coupon">
                            <option value="">请选择使用类型</option>
                            <option value="1" @if( isset( $info['zhekou']['type_shop'] ) && $info['zhekou']['type_shop'] == 1 ) selected="selected" @endif>普通商品</option>
                            <option value="2" @if( isset( $info['zhekou']['type_shop'] ) && $info['zhekou']['type_shop'] == 2 ) selected="selected" @endif>服务商品</option>
                            <option value="3" @if( isset( $info['zhekou']['type_shop'] ) && $info['zhekou']['type_shop'] == 3 ) selected="selected" @endif>保养套餐</option>
                        </select>
                    </div>
                    <div>
                        <table class="layui-table layui-form">
                            <thead>
                            <tr>
                                <th>选择</th>
                                <th>优惠券名称</th>
                                <th>赠送数量（张）</th>
                                <th>有效期限（天）</th></tr>
                            </thead>
                            <tbody id="zhekou_tr">
                            @if( isset( $info['zhekou'] ) )
                                @foreach( $info['zhekou']['coupons'] as $key=>$vo)
                                    <tr class="phono_ine">
                                        <td><input type="checkbox" name="zhekou[]" value="{{$vo['id']}}" @if( !empty( $vo['number']) ) checked="checked" @endif lay-skin="primary" placeholder="赠送张数" autocomplete="off"></td>
                                        <td>{{$vo['name']}}</td>
                                        <td><input type="number" min="1" name="zhekou_number[]" placeholder="赠送张数" value="{{$vo['number']}}" autocomplete="off" class="layui-input"></td>
                                        <td><input type="number" min="1"  name="zhekou_validity_at[]" placeholder="有效期限" value="{{$vo['validity_at']}}" autocomplete="off" class="layui-input"></td>
                                    </tr>
                                @endforeach
                            @endif
                            </tbody>
                        </table>
                    </div>
                </div>
                <hr/>
                <div class="layui-form-item">
                    <label class="layui-form-label">洗车券</label>
                    <div>
                        <table class="layui-table layui-form">
                            <thead>
                            <tr>
                                <th>选择</th>
                                <th>优惠券名称</th>
                                <th>赠送数量（张）</th>
                                <th>有效期限（天）</th></tr>
                            </thead>
                            <tbody id="xiche_tr">
                                @foreach($data as $key=>$value)
                                    <tr class="phono_ine">
                                        <td><input type="checkbox" name="xiche[]" value="{{$value['id']}}" @if( !empty( $value['number'] ) ) checked="checked" @endif lay-skin="primary" placeholder="赠送张数" autocomplete="off"></td>
                                        <td>{{$value['name']}}</td>
                                        <td><input type="number" min="1" name="xiche_number[]" placeholder="赠送张数"  @if( !empty( $value['number'] ) ) value="{{$value['number']}}" @endif  autocomplete="off" class="layui-input"></td>
                                        <td><input type="number" min="1"  name="xiche_validity_at[]" placeholder="有效期限" @if( !empty( $value['validity_at'] ) ) value="{{$value['validity_at']}}" @endif autocomplete="off" class="layui-input"></td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-form-label" >
                        <button class="layui-btn" lay-submit="" lay-filter="btn_edit">确认提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>



@endsection

@section('tab_js')

    <script>
        layui.use(['form', 'layer', 'jquery','laydate'], function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;
            var laydate = layui.laydate;
            var load = '';
            var manjian_type_shop = "{{{$info['manjian']['type_shop']}}}"
              , zhekou_type_shop = "{{{$info['zhekou']['type_shop']}}}"
              , type_age = "{{{$info['age']}}}";

            //年限
            form.on('select(age)',function( data ){

                load = layer.load(1); //添加laoding,0-2两种方式
                type_age = data.value ;
                get_gift_coupons( manjian_type_shop , 2 , type_age );
                get_gift_coupons( zhekou_type_shop , 3 , type_age );
                get_gift_coupons( '' , 1 , type_age );

            });



            //满减
            form.on('select(manjian_type_coupon)',function( data ){
                let type_shop = data.value ;
                manjian_type_shop = type_shop;
                get_gift_coupons( type_shop , 2 , type_age);
            });


            function get_gift_coupons( type_shop = '' , pid = '' ,age = '' ){
                let type = "{{{$type}}}" , str = '';
                $.ajax({
                    url: js_url('admin-gift-get-coupons'),
                    type: 'get',
                    data: {type_shop:type_shop,pid:pid,type:type , type_age:age},
                    dataType: "json",
                    success: function (result) {
                        if (result.state === 0) {
                            $.each(result.data,function(index,value){
                                if( pid == 1 ){
                                    str += '<tr class="phono_ine">' +
                                        '<td><input type="checkbox" name="xiche[]" value="' + value.id + '" lay-skin="primary" title="" ';
                                    if (value['number']) {
                                        str += 'checked="checked"';
                                    }
                                    str += '></td>' +
                                        '<td>' + value.name + '</td>' +
                                        '<td><input type="number" min="1" name="xiche_number[]" placeholder="赠送张数" value="' + value.number + '" autocomplete="off" class="layui-input"></td>' +
                                        '<td><input type="number" min="1"  name="xiche_validity_at[]" placeholder="有效期限" value="' + value.validity_at + '" autocomplete="off" class="layui-input"></td>' +
                                        '</tr>';
                                }else if( pid == 2  ) {
                                    str += '<tr class="phono_ine">' +
                                        '<td><input type="checkbox" name="manjian[]" value="' + value.id + '" lay-skin="primary" title="" ';
                                    if (value['number']) {
                                        str += 'checked="checked"';
                                    }
                                    str += '></td>' +
                                        '<td>' + value.name + '</td>' +
                                        '<td><input type="number" min="1" name="manjian_number[]" placeholder="赠送张数" value="' + value.number + '" autocomplete="off" class="layui-input"></td>' +
                                        '<td><input type="number" min="1"  name="manjian_validity_at[]" placeholder="有效期限" value="' + value.validity_at + '" autocomplete="off" class="layui-input"></td>' +
                                        '</tr>';
                                }else if(  pid == 3 ){
                                    str += '<tr class="phono_ine">'+
                                        '<td><input type="checkbox" name="zhekou[]" value="'+value.id+'" lay-skin="primary" title="" ';
                                    if( value['number'] ){
                                        str += 'checked="checked"';
                                    }
                                    str += '></td>'+
                                        '<td>'+value.name+'</td>'+
                                        '<td><input type="number" min="1" name="zhekou_number[]" placeholder="赠送张数" value="'+value.number+'" autocomplete="off" class="layui-input"></td>'+
                                        '<td><input type="number" min="1"  name="zhekou_validity_at[]" placeholder="有效期限" value="'+value.validity_at+'" autocomplete="off" class="layui-input"></td>'+
                                        '</tr>';
                                }
                            });　　　　　　　　　
                            if( pid == 1) {　　　　　　　　　　　　　//这里注意一下 不知道是自己看文档不仔细，还是习惯了jequry的用法  要用html()才可以 使用append() 添加动态数据无效
                                $("#xiche_tr").html( str );
                            }else if( pid == 2  ) {
                                $("#manjian_tr").html( str );
                            }else if(  pid == 3 ){
                                $("#zhekou_tr").html( str );
                            }
                            layer.close(load);    //返回数据关闭loading
                            form.render();    //重新渲染
                        } else {
                            layer.msg(result.msg, {time: 2000, icon: 5});
                        }
                    }
                });
            }



            //折扣
            form.on('select(zhekou_type_coupon)',function( data ){
                let type_shop = data.value , str = '' , type = "{{{$type}}}";
                zhekou_type_shop = type_shop;
                get_gift_coupons( type_shop , 3 , type_age);
            });


            //监听提交
            form.on('submit(btn_edit)', function (data) {
                $.ajax({
                    url: js_url('admin-gift-store'),
                    type: 'post',
                    data: data.field,
                    dataType: "json",
                    success: function (result) {
                        if (result.state === 0) {
                            layer.msg(result.msg, {time: 600, icon: 6}, function () {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                        } else {
                            layer.msg(result.msg, {time: 2000, icon: 5});
                        }
                    }
                });
                return false;
            });
        });
    </script>




@endsection